package com.emm.yixun.system.service.impl.manage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.MessageReceiverService;
import com.emm.yixun.basic.service.NoticeService;
import com.emm.yixun.basic.service.NoticeTemporaryService;
import com.emm.yixun.basic.service.ProjectService;
import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.model.Notice;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.dto.MessageReceiverDto;
import com.emm.yixun.common.model.dto.NoticeDto;
import com.emm.yixun.common.model.dto.NoticeTemporaryDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.IsSendEnum;

@Service("customerCarePushMsgCurrManage")
public class CustomerCarePushMsgCurrManage {

	@Autowired
	private MessageReceiverService messageReceiverService;
	@Autowired
	private AppPushService appPushService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private NoticeTemporaryService noticeTemporaryService;
	@Autowired
	private ProjectService projectService;
	
	/**
	 * 推送客户关怀消息
	 * 当天定时通知: 推送当天过生日、纪念日的客户消息
	 * @param reqDto
	 * @return
	 */
	public CommonResDTO pushMsg() {
		CommonResDTO resDto = new CommonResDTO();
		resDto.setResult("0");
		try{
			//1、查询客户关怀相关推送消息
			// NT09: 客户关怀,今天过生日的客户消息,定时提醒
			// NT11: 客户关怀,今天过纪念日的客户消息,定时提醒
			List<MessageReceiverDto> msgRecList = new ArrayList<MessageReceiverDto>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 		Calendar calendar = Calendar.getInstance();
	 		int year = calendar.get(Calendar.YEAR);
	 		int month = calendar.get(Calendar.MONTH) + 1;
	 		int day = calendar.get(Calendar.DAY_OF_MONTH);
	 		int hour = calendar.get(Calendar.HOUR_OF_DAY);
	 		int minute = calendar.get(Calendar.MINUTE);
	 		String timerNoticeBefore = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":00"; 
	 		String timerNoticeAfter = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":59"; 
	 		//查询客户关怀,生日、未推送且定時推送时间在当前的消息
	 		MessageReceiverDto mrDto = new MessageReceiverDto();
	 		mrDto.setNoticeTempType("NT39");
	 		mrDto.setIsSend(IsSendEnum.UNSEND.getNo());
	 		//mrDto.setTimerNoticeBefore(sdf.parse(timerNoticeBefore));
	 		mrDto.setTimerNoticeAfter(sdf.parse(timerNoticeAfter));
	   	 	List<MessageReceiverDto> mrBirthList = messageReceiverService.selectPushMsgListByDto(mrDto);
	   	 	if(mrBirthList != null){
	   	 		for(MessageReceiverDto mrBirth: mrBirthList){
	   	 			msgRecList.add(mrBirth);
	   	 		}
	   	 	}
	   	 	//查询客户关怀,纪念日、未推送且定時推送时间在当前的消息
	   	 	mrDto = new MessageReceiverDto();
	 		mrDto.setNoticeTempType("NT41");
	 		mrDto.setIsSend(IsSendEnum.UNSEND.getNo());
	 		//mrDto.setTimerNoticeBefore(sdf.parse(timerNoticeBefore));
	 		mrDto.setTimerNoticeAfter(sdf.parse(timerNoticeAfter));
	   	 	List<MessageReceiverDto> mrWeddingList = messageReceiverService.selectPushMsgListByDto(mrDto);
	   	 	if(mrBirthList != null){
	   	 		for(MessageReceiverDto mrWedding: mrWeddingList){
	   	 			msgRecList.add(mrWedding);
	   	 		}
	   	 	}
	   	 	
	   	 	if(msgRecList != null && msgRecList.size() > 0){
	   	 		String noticeIds = "";
	   	 		String msgRecIds = "";
	   	 		Set<String> noticeIdSet = new HashSet<String>();
	   	 		for(MessageReceiverDto msgRecDto: msgRecList){
	   	 			//开始推送消息
//		   	 		Map<String, String> pushMap = new HashMap<String, String>();
//			   	 	pushMap.put("action", "客户关怀定时提醒");
//			   	 	pushMap.put("remindCategoryId", String.valueOf(msgRecDto.getDictionaryId()));
//			   	 	pushMap.put("pushType", "KHGHDSTX");
//					String context = msgRecDto.getContent();
//					appPushService.pushMessage(String.valueOf(msgRecDto.getReceiverId()), context,
//						msgRecDto.getTitle(), pushMap);
	   	 			Notice notice = noticeService.selectById(msgRecDto.getNoticeId()); //查询客户项目code
	   	 			ProjectDto proDto = new ProjectDto();
	   	 			proDto.setProjectCode(notice.getProjectCode());
	   	 			Project project = projectService.selectByDto(proDto); //查询项目名称
	   	 			
					NoticeTemporaryDto entity = new NoticeTemporaryDto();
	   	 		    entity.setTitle("客户关怀定时提醒");
	   	 		    entity.setContent(msgRecDto.getContent());
	   	 		    entity.setReceiverId(msgRecDto.getReceiverId());
	   	 		    entity.setAction("客户关怀定时提醒");
	   	 		    entity.setPushType("KHGHDSTX");
	   	 		    entity.setMerchantId(project.getMerchantId());
	   	 		    entity.setProjectCode(project.getProjectCode());
	   	 		    entity.setProjectName(project.getProjectName());
	   	 		    entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
	   	 			noticeTemporaryService.create(entity);
					
					//消息ID,Set
					noticeIdSet.add(String.valueOf(msgRecDto.getNoticeId()));
					//消息接收人ID'S
					if(StringUtils.isNotEmpty(msgRecIds)){
						msgRecIds += ",";
					}
					msgRecIds += String.valueOf(msgRecDto.getId());
	   	 		}
	   	 		//需更新消息ID'S
	   	 		if(!noticeIdSet.isEmpty()){
					for(String noticeId: noticeIdSet){
						if(StringUtils.isNotEmpty(noticeIds)){
							noticeIds += ",";
						}
						noticeIds += noticeId;
		   	 		}
	   	 		}
	   	 		//更新消息
	   	 		NoticeDto noticeDto = new NoticeDto();
	   	 		noticeDto.setIsSend(IsSendEnum.SEND.getNo());
	   	 		noticeService.updates(noticeDto, noticeIds);
	   	 		//更新接收人
	   	 		MessageReceiverDto updateMRDto = new MessageReceiverDto();
	   	 		updateMRDto.setIsSend(IsSendEnum.SEND.getNo());
	   	 		messageReceiverService.updates(updateMRDto, msgRecIds);
	   	 	}
	   	 	resDto.setResult("1");
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("推送当天客户关怀提醒消息失败!");
			resDto.setResult("0");
			return resDto;
		}
		return resDto;
	}
	
}
